const express = require('express');
const path = require('path');
const proxy = require('http-proxy-middleware');

const app = express();

// http请求的IP
let serverUrl = 'https://targetURL';

// 设置静态文件路径
app.use(express.static(path.join(__dirname, './')));

// 解决跨域问题
app.all('*', function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header(
    'Access-Control-Allow-Headers',
    'Content-Type,Content-Length, Authorization, Accept,X-Requested-With'
  );
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
  if (req.method == 'OPTIONS') {
    res.send(200);
  } else {
    next();
  }
});

let options = {
  target: serverUrl,
  changeOrigin: true
  // pathRewrite: {
  //   '^/': ''
  // }
};

const exampleProxy = proxy(options);
app.use('/', exampleProxy); //  ‘/’ 表示对所有请求代理

app.listen(8092, function() {
  console.log('server is running at port 8092');
});
